// prop_player

local prop_player = Assault.Entities:New("prop_player")

// On entity think

function prop_player:OnEntityThink()
	local Position = self:EntityGetPosition()
	
	// Mouse X and Mouse Y
	
	local X = gui.MouseX()
	local Y = gui.MouseY()
	
	// Vector
	
	local Vector = Assault.Vector:New(X, Y)
	
	// Vector
	
	Vector = Position - Vector
	
	// Calculate rotation
	
	local Rotation = math.deg(math.atan2(Vector.X, Vector.Y))
	
	// Set entity rotation
	
	self:EntitySetRotation(Rotation)
	
	// Player is holding down the forward key
	
	if (Assault.Client:KeyDown(IN_FORWARD)) then
		local Sin = math.sin(math.rad(Rotation)) * 2.5
		local Cos = math.cos(math.rad(Rotation)) * 2.5
		
		// X and Y
		
		local X = Position.X - Sin
		local Y = Position.Y - Cos
		
		// Vector
		
		local Vector = Assault.Vector:New(X, Y)
		
		// Entity check for collisions
		
		local Collision = self:EntityCheckForCollisions(Vector)
		
		// Check if the new position will collide with any other entities
		
		if (Collision) then return false end
		
		// Set entity position
		
		self:EntitySetPosition(Vector)
	end
end

// On player mouse pressed

function prop_player:OnPlayerMousePressed(Code)
	local Position = self:EntityGetPosition()
	
	// Check for the left mouse button
	
	if (Code == MOUSE_LEFT) then
		self:EntityEmitSound("weapons/pistol/pistol_fire2.wav", self)
		
		// Entity shoot bullets
		
		self:EntityShootBullets(5, 1, 0, 10, 50, Color(255, 215, 0, 255))
	end
end

// On entity break

function prop_player:OnEntityBreak()
	local Position = self:EntityGetOrigin()
	
	// Blood
	
	Assault.Entities.EntitiesQuickCreate("effect_sparks", Position, false, false, Color(255, 50, 50, 255))
end

// On player key press

function prop_player:OnPlayerKeyPress(Key)
	print(Key)
	if (Key == IN_USE) then
		local X = gui.MouseX()
		local Y = gui.MouseY()
		
		// Vector
		
		local Vector = Assault.Vector:New(X, Y)
		
		// Check for collisions
		
		local Collision = self:EntityCheckForCollisions(Vector)
		
		// Check if there was a collision
		
		if (Collision) then
			// Entity get distance
			
			local Len = self:EntityGetDistance(Collision)
			
			print(Len)
			
			// Check length
			
			if (Len < 64) then
				Collision:OnEntityUsed(self)
			end
		end
	end
end

// Create

prop_player:Create()